package com.HMI;

import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import com.Resources.CustomColor;

@SuppressWarnings("serial")
public class GraphWindow extends AlphaPanel {

	static List<Integer> yCoord;
	
	private Draw graph;
	private int count;
	
	private AlphaPanel graphPanel;
	private AlphaPanel summaryPanel;
	private PanelText topRowValue;
	private PanelText bottomRowValue;
	
	private PanelText timeLabel1;
	private PanelText timeLabel2;
	private PanelText timeLabel3;
	private PanelText timeLabel4;
	private PanelText timeLabel5;
	private PanelText timeLabel6;
	private PanelText timeLabel7;
	private PanelText timeLabel8;
	
	private String[] xAxisTimeLabels = new String[8];
	
	public GraphWindow(int x, int y, int width, int height) {
		super(x, y, width, height);
		this.setBackground(CustomColor.TRANSPARENT);
		initializeGraphPanel();
		initializeSummaryPanel();
	}
	
	private void initializeGraphPanel() {
		graphPanel = new AlphaPanel(40, 40, 502, 265);
		populateGraphPanel();
		this.add(graphPanel);
	}
	
	private void populateGraphPanel() {
//		graph = new Draw();
//		graph.setBounds(56, 14, 401, 205);
//		graph.setBackground(CustomColor.WHITE);
//		graphPanel.add(graph);
//		yCoord = new ArrayList<Integer>();
//		for(int i=0; i<420; i++) {
//			yCoord.add((int) Math.round(100*Math.sin(i*Math.PI/180)));
//		}
//		Timer timer = new Timer();
//	    timer.schedule(new RemindTask(),
//	                       0,        //initial delay
//	                       16);  //subsequent rate
//		graph.drawing(yCoord);
//		count = 59;
		initializeXaxisLabels();
	}
	

	private void initializeXaxisLabels() {
		PanelText xAxisLabel = new PanelText("Time of day", 0, 235, 502, 30, SwingConstants.CENTER, SwingConstants.CENTER);
		xAxisLabel.setFont(new Font("DejaVu Sans", Font.PLAIN, 19));
		graphPanel.add(xAxisLabel);
		
		timeLabel1 = new PanelText(":01", 17, 220, 60, 30, SwingConstants.RIGHT, SwingConstants.TOP);
		timeLabel1.setFont(new Font("DejaVu Sans", Font.PLAIN, 14));
		graphPanel.add(timeLabel1);
		
		timeLabel2 = new PanelText(":02", 74, 220, 60, 30, SwingConstants.RIGHT, SwingConstants.TOP);
		timeLabel2.setFont(new Font("DejaVu Sans", Font.PLAIN, 14));
		graphPanel.add(timeLabel2);
		
		timeLabel3 = new PanelText(":03", 131, 220, 60, 30, SwingConstants.RIGHT, SwingConstants.TOP);
		timeLabel3.setFont(new Font("DejaVu Sans", Font.PLAIN, 14));
		graphPanel.add(timeLabel3);
		
		timeLabel4 = new PanelText(":04", 188, 220, 60, 30, SwingConstants.RIGHT, SwingConstants.TOP);
		timeLabel4.setFont(new Font("DejaVu Sans", Font.PLAIN, 14));
		graphPanel.add(timeLabel4);
		
		timeLabel5 = new PanelText(":05", 246, 220, 60, 30, SwingConstants.RIGHT, SwingConstants.TOP);
		timeLabel5.setFont(new Font("DejaVu Sans", Font.PLAIN, 14));
		graphPanel.add(timeLabel5);
		
		timeLabel6 = new PanelText(":06", 303, 220, 60, 30, SwingConstants.RIGHT, SwingConstants.TOP);
		timeLabel6.setFont(new Font("DejaVu Sans", Font.PLAIN, 14));
		graphPanel.add(timeLabel6);
		
		timeLabel7 = new PanelText(":07", 360, 220, 60, 30, SwingConstants.RIGHT, SwingConstants.TOP);
		timeLabel7.setFont(new Font("DejaVu Sans", Font.PLAIN, 14));
		graphPanel.add(timeLabel7);
		
		timeLabel8 = new PanelText(":08", 418, 220, 60, 30, SwingConstants.RIGHT, SwingConstants.TOP);
		timeLabel8.setFont(new Font("DejaVu Sans", Font.PLAIN, 14));
		graphPanel.add(timeLabel8);
		
		updateXaxisTimeLabels();
		
	}
	
	public void updateXaxisTimeLabels() {
		Calendar cal = Calendar.getInstance();
		
		for(int i=xAxisTimeLabels.length-1; i>=0; i--) {
			xAxisTimeLabels[i] = new SimpleDateFormat("h:mm").format(cal.getTime());
			cal.add(Calendar.HOUR, -2);
		}
		timeLabel1.setText(xAxisTimeLabels[0]);
		timeLabel2.setText(xAxisTimeLabels[1]);
		timeLabel3.setText(xAxisTimeLabels[2]);
		timeLabel4.setText(xAxisTimeLabels[3]);
		timeLabel5.setText(xAxisTimeLabels[4]);
		timeLabel6.setText(xAxisTimeLabels[5]);
		timeLabel7.setText(xAxisTimeLabels[6]);
		timeLabel8.setText(xAxisTimeLabels[7]);
	}
	
	public void drawGraph() {
		count++;
		if(count == 360) {
			count = 0;
		}
		yCoord.remove(0);
		yCoord.add((int) Math.round(100*Math.sin(count*Math.PI/180)));
		graph.drawing(yCoord);
	}
	
	private void initializeSummaryPanel() {
		summaryPanel = new AlphaPanel(40, 334, 502, 118);
		initializeTopRowValue();
		initializeBottomRowValue();
		this.add(summaryPanel);
	}

	protected void labelTopRow(String label) {
		PanelText lblTopRow = new PanelText(label, 22, 29,
				215, 29, SwingConstants.LEFT, SwingConstants.BOTTOM);
		summaryPanel.add(lblTopRow);
	}
	
	protected void labelBottomRow(String label) {
		PanelText lblBottomRow = new PanelText(label, 22, 64, 215,
				29, SwingConstants.LEFT, SwingConstants.BOTTOM);
		summaryPanel.add(lblBottomRow);
	}

	private void initializeTopRowValue() {
		topRowValue = new PanelText("", 205, 32, 275, 27,
				SwingConstants.RIGHT, SwingConstants.BOTTOM);
		summaryPanel.add(topRowValue);
	}

	private void initializeBottomRowValue() {
		bottomRowValue = new PanelText("", 305, 64,
				175, 27, SwingConstants.RIGHT, SwingConstants.BOTTOM);
		summaryPanel.add(bottomRowValue);
	}
	
	protected void setTopRowValue(String value) {
		topRowValue.setText(value);
	}
	
	protected void setBottomRowValue(String value) {
		bottomRowValue.setText(value);
	}
	
	protected void addGraph(String resource) {
			try {
				ClassLoader classLoader = Thread.currentThread()
						.getContextClassLoader();
				InputStream input = classLoader
						.getResourceAsStream(resource);
				Image logo = ImageIO.read(input);			
				JLabel graph = new JLabel(new ImageIcon(logo));
				graph.setBounds(0, 0, 457, 219);
				graphPanel.add(graph);	
			} catch (IOException e) {
				e.printStackTrace();
			}
	}
	
	class RemindTask extends TimerTask {
		
		public RemindTask() {
		}
		
	    public void run() {
	        drawGraph();
	    }
	}
}